package com.ljw.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljw.entity.User;
import com.ljw.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author ljw
 * @since 2020-07-25
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/save")
    public boolean save(@RequestBody User user){
        String userPhone = user.getUserPhone();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_phone",userPhone);
        User one = userService.getOne(queryWrapper);
        if(null != one)
            return false;
        return userService.save(user);
    }

    @GetMapping("/findAll")
    public IPage<User> findAll(@RequestParam int current,@RequestParam int size){
        Page<User> page = new Page<>(current,size);
        return userService.findAll(page);
    }

    @PostMapping("/login")
    public User login(@RequestBody User user){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_phone", user.getUserPhone()).eq("user_password",user.getUserPassword());
        return userService.getOne(queryWrapper);
    }

    @PostMapping("/updateUser")
    public void update(@RequestBody User user){
        userService.updateById(user);
    }

}

